package com.akilesa.dictcreator;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: akilesa
 * Date: 19.01.2010
 * Time: 11:25:31
 * To change this template use File | Settings | File Templates.
 */
public class EnglishWordTokenizer {

    private static final String regexp="[a-zA-Z]+"; 

    public EnglishWordTokenizerResult divideOnWords(String text){

        //TODO optimize it

        EnglishWordTokenizerResult result=new EnglishWordTokenizerResult();
        Map<String,Long> map=new HashMap<String,Long>();
        Pattern pattern=Pattern.compile(regexp);
        Matcher matcher=pattern.matcher(text);
        while(matcher.find()){
            String w=matcher.group().toLowerCase();
            Long count=map.get(w);
            if(count!=null){
                map.put(w,++count);
            }else{
                map.put(w,1L);
            }
        }
        List<String> list=new ArrayList<String>();
        list.addAll(map.keySet());
        Collections.sort(list);
        for(String item : list){
            EnglishWordTokenizerResult.WordDescription word=
                    new EnglishWordTokenizerResult.WordDescription();
            word.setCount(map.get(item));
            word.setWord(item);
            result.getWords().add(word);
        }
        Statistic.getInstance().setWordTotalCount(result.getTotal());
        return result;
    }
}
